<template>
  <el-dialog append-to-body width="900px" title="选择材料" :visible.sync="dialogVisible" @close="handleClose">
    <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
      <el-form-item label="材料名称" prop="name">
        <el-input v-model="page.query.name" placeholder="请输入材料名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="table"
      v-loading="tableLoading"
      :data="tableData"
      style="width: 100%"
      @row-dblclick="tableDbClick"
      @select="handleSelect"
    >
      <el-table-column type="selection" width="45" />
      <el-table-column label="材料名称" prop="name" />
      <el-table-column label="含税单价" prop="unitPrice" />
      <el-table-column label="单位" prop="unit">
        <template slot-scope="scope">
          <dict-table-column v-model="scope.row.unit" dict-key="bom_unit" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <span slot="footer" class="dialog-footer">
      <el-button v-loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button v-loading="formLoading" type="primary" @click="handleChoose(selected)">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { selectMaterialPage } from '@/api/erp/material'

export default {
  name: 'MaterialModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      formLoading: false,
      selected: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          name: ''
        }
      },
      dialogVisible: this.visible
    }
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectMaterialPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelect(row) {
      this.selected = []
      if (row.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(row[row.length - 1])
      }
      this.selected = [row[row.length - 1]]
    },
    tableDbClick(row) {
      this.$emit('close', row)
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.doPage()
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleClose() {
      this.dialogVisible = false
      this.$emit('close', null)
      this.selected = []
    },
    handleChoose(row) {
      if (row.length === 0) {
        this.$message.warning('请勾选物料')
        return
      }
      this.$emit('close', this.selected[0])
    }
  }
}
</script>

<style scoped>

</style>
